import Vue from 'vue'
import Router from 'vue-router'
import blog from "../components/blog/blog.vue"
import blogIndex from "../components/blog/blogIndex.vue"
Vue.use(Router)

// 解决ElementUI导航栏中的vue-router在3.0版本以上重复点菜单报错问题
const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

const User = {
  props: ['id'],

}

const router = new Router({
  linkActiveClass: "active",
  routes: [
    {
      path: '/',
      redirect:"/blog",
    },
    {
      path:"/blog",
      redirect:"/blog/index",
      component:blog,
      
      children:[
        {path:'index',component:()=>import('../components/blog/blogIndex.vue')},//全部博客
        {path:'add',component:()=>import('../components/blog/blogAdd.vue')},//添加
        {path:'myblog',component:()=>import('../components/blog/myblog.vue')},//个人博客
        {path:'blogsearch/:mothed',name:'blogsearch',component:()=>import('../components/blog/blogsearch.vue')},//查询博客页面
        {path:'update',component:()=>import('../components/blog/blogUpdate.vue')},//跟新我的博客
        {path:'mine',component:()=>import('../components/user/mine/mine.vue')},//查看我的信息
        {path:'blogLook/',component:()=>import('../components/blog/blogLook.vue')},
      ]

    },
    {
      path:'/login',
      name:'login',
      component:()=> import ('../components/user/login/login.vue'),
    },
    {
      path:'/register',//注册
      name:'register',
      component:()=> import ('../components/user/register/register.vue'),
    },
    {
      path:'/forgetPwd',//忘记密码
      name:'forgetPwd',
      component:()=> import ('../components/user/forgetPwd/forgetPwd.vue'),
    },
  ]
})


/**
 * 路由守卫
 */
// router.beforeEach((to,from,next)=>{
//   const user = localStorage.getItem("user")
//   console.log("beforeEach--------------------------")
//   console.log(to,from)
//   // var user = null
//   console.log(user)
//   console.log(user===null) //true
//   if( user===null && to.path != '/login'){
    
//     next({
//       path:'/login',
//     })
//   }
//   else{
//     next()
//   }
// });

export default router